package cn.lizemin.toExcel;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: lizemin
 * @version: 1.0
 */
public class MyUtil_third {

    @Test
    public void test1() throws IOException {
//        Create an HSSFWorkbook object
        HSSFWorkbook wb = new HSSFWorkbook();
// / / Create an HSSFSheet object
        HSSFSheet sheet = wb.createSheet("sheet0");
// / / Create an HSSFRow object

        HSSFRow row1 = sheet.createRow(0);
// / / Create an HSSFCell object
        HSSFCell cell = row1.createCell(0);
// / / Set the value of the cell

        cell.setCellValue("Chinese in cell");

        row1.createCell(1).setCellValue("数学");

        sheet.createRow(1).createCell(1).setCellValue(98);
// / / Output Excel file
        FileOutputStream output = new FileOutputStream("C:\\Users\\15871\\Desktop\\mybatis\\lizemin.xls");
        wb.write(output);

        output.flush();

    }

    @Test
    public void test2() throws IOException {
        HSSFWorkbook wb = new HSSFWorkbook();
// / / Create an HSSFSheet object
        HSSFSheet sheet = wb.createSheet("lizemin");
        Student stu1 = new Student("lizemin",29);
        Student stu2 = new Student("lize",18);
        Student stu3 = new Student("min",38);
        List<Student> students = new ArrayList<>();
        students.add(stu1);
        students.add(stu2);
        students.add(stu3);

// / / Create an HSSFCell object


        HSSFRow row1 = sheet.createRow(0);
//        sheet.createRow(i).createCell(i).setCellValue();
        int length = stu1.getClass().getDeclaredFields().length;
        Field[] declaredFields = stu1.getClass().getDeclaredFields();
        for (int j = 0; j < length; j++) {
            HSSFCell cell = row1.createCell(j);
            cell.setCellValue(String.valueOf(declaredFields[j].getName()));
        }

        //先打印一行数据
        /*HSSFRow row2 = sheet.createRow(1);
        row2.createCell(0).setCellValue(stu1.getName());
        row2.createCell(1).setCellValue(stu1.getAge());*/

        //多行
        for (int i = 0; i < students.size(); i++) {
            HSSFRow row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(students.get(i).getName());
            row.createCell(1).setCellValue(students.get(i).getAge());

        }


// / / Create an HSSFRow object

//        HSSFRow row1 = sheet.createRow(0);
// / / Create an HSSFCell object
        /*HSSFCell cell = row1.createCell(0);
// / / Set the value of the cell

        cell.setCellValue("Chinese in cell");

        row1.createCell(1).setCellValue("数学");*/

//        sheet.createRow(1).createCell(1).setCellValue(98);
// / / Output Excel file
        FileOutputStream output = new FileOutputStream("C:\\Users\\15871\\Desktop\\mybatis\\lizemin2.xls");
        wb.write(output);

        output.flush();

    }

}
